library(survey)
library(ggplot2)

## see PCM_cleaning.R for data cleaning
load("~/Dropbox/Public_Conf_Mil/Data_and_code/clean_data.RData")

## Create weighted survey design object
milconf_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.df
  )

addline_format <- function(x,...){
  gsub('\\s','\n',x)
}

#### CONFIDENCE: Q8 BY CONDITION ####

Q8_diff <- matrix(NA, ncol = 3, nrow = 7)

## Create difference in means for each condition

diff_Q8_2 <- svyby(~ Q8_d, ~ ASSIGNMENT_A_2, milconf_design, svymean, na.rm = TRUE)
Q8_diff[1,1] <- diff_Q8_2[2,2] - diff_Q8_2[1,2]
Q8_diff[1,2] <- confint(diff_Q8_2)[2,1] - confint(diff_Q8_2)[1,1]
Q8_diff[1,3] <- confint(diff_Q8_2)[2,2] - confint(diff_Q8_2)[1,2]

diff_Q8_3 <- svyby(~ Q8_d, ~ ASSIGNMENT_A_3, milconf_design, svymean, na.rm = TRUE)
Q8_diff[2,1] <- diff_Q8_3[2,2] - diff_Q8_3[1,2]
Q8_diff[2,2] <- confint(diff_Q8_3)[2,1] - confint(diff_Q8_3)[1,1]
Q8_diff[2,3] <- confint(diff_Q8_3)[2,2] - confint(diff_Q8_3)[1,2]

diff_Q8_4 <- svyby(~ Q8_d, ~ ASSIGNMENT_A_4, milconf_design, svymean, na.rm = TRUE)
Q8_diff[3,1] <- diff_Q8_4[2,2] - diff_Q8_4[1,2]
Q8_diff[3,2] <- confint(diff_Q8_4)[2,1] - confint(diff_Q8_4)[1,1]
Q8_diff[3,3] <- confint(diff_Q8_4)[2,2] - confint(diff_Q8_4)[1,2]

diff_Q8_5 <- svyby(~ Q8_d, ~ ASSIGNMENT_A_5, milconf_design, svymean, na.rm = TRUE)
Q8_diff[4,1] <- diff_Q8_5[2,2] - diff_Q8_5[1,2]
Q8_diff[4,2] <- confint(diff_Q8_5)[2,1] - confint(diff_Q8_5)[1,1]
Q8_diff[4,3] <- confint(diff_Q8_5)[2,2] - confint(diff_Q8_5)[1,2]

diff_Q8_6 <- svyby(~ Q8_d, ~ ASSIGNMENT_A_6, milconf_design, svymean, na.rm = TRUE)
Q8_diff[5,1] <- diff_Q8_6[2,2] - diff_Q8_6[1,2]
Q8_diff[5,2] <- confint(diff_Q8_6)[2,1] - confint(diff_Q8_6)[1,1]
Q8_diff[5,3] <- confint(diff_Q8_6)[2,2] - confint(diff_Q8_6)[1,2]

diff_Q8_7 <- svyby(~ Q8_d, ~ ASSIGNMENT_A_7, milconf_design, svymean, na.rm = TRUE)
Q8_diff[6,1] <- diff_Q8_7[2,2] - diff_Q8_7[1,2]
Q8_diff[6,2] <- confint(diff_Q8_7)[2,1] - confint(diff_Q8_7)[1,1]
Q8_diff[6,3] <- confint(diff_Q8_7)[2,2] - confint(diff_Q8_7)[1,2]

diff_Q8_8 <- svyby(~ Q8_d, ~ ASSIGNMENT_A_8, milconf_design, svymean, na.rm = TRUE)
Q8_diff[7,1] <- diff_Q8_8[2,2] - diff_Q8_8[1,2]
Q8_diff[7,2] <- confint(diff_Q8_8)[2,1] - confint(diff_Q8_8)[1,1]
Q8_diff[7,3] <- confint(diff_Q8_8)[2,2] - confint(diff_Q8_8)[1,2]

## Plot
Q8_plot.df <- data.frame(change = Q8_diff[,1],
                         ci_low = Q8_diff[,2],
                         ci_hi = Q8_diff[,3],
                         cond = c(1:7))

Q8_plot <- ggplot(Q8_plot.df) + 
  geom_pointrange(aes(x = cond, y = change, ymin = ci_low, ymax = ci_hi), fatten = 2, size = 0.5) +
  geom_text(aes(x = cond, y = change, label = sprintf("%0.2f", round(change, digits = 2))), size = 3,
            vjust = 2.5)
Q8_plot <- Q8_plot + xlab("Treatment Condition") + 
  scale_x_continuous(breaks = c(1:7),
                     labels = addline_format(c("Republican Military", "Democrat Military",
                                               "Polarized Military", "Lost Afghanistan War",
                                               "Won Afghanistan War", "Ethical Generals",
                                               "Unethical Generals"))) +
  ylab("Difference in Means from Control Condition") + ggtitle("Q8 by Treatment Condition")
Q8_plot <- Q8_plot + geom_hline(yintercept = 0, colour = "red", lty = 1)
Q8_plot + theme_bw()

## Second plot - partisan conditions only (for Peter's Oxford talk)
Q8_plot2.df <- Q8_plot.df[1:3,]

Q8_plot2 <- ggplot(Q8_plot2.df) + 
  geom_pointrange(aes(x = cond, y = change, ymin = ci_low, ymax = ci_hi), fatten = 2, size = 0.5) +
  geom_text(aes(x = cond, y = change, label = sprintf("%0.2f", round(change, digits = 2))), 
            size = 3,
            vjust = 2.5)
Q8_plot2 <- Q8_plot2 + xlab("Treatment Condition") + 
  scale_x_continuous(breaks = c(1:3),
                     labels = addline_format(c("Republican Military", "Democrat Military",
                                               "Polarized Military"))) +
  ylab("Difference in Means from Control Condition") + ylim(-0.1, 0.1) +
  geom_hline(yintercept = 0, colour = "red", lty = 1) + theme_bw()
ggsave("~/Dropbox/Public_Conf_Mil/Wave 1 Figures/Q8_partisan.jpeg", 
       plot = Q8_plot2, dpi = 1000)

#### CONFIDENCE: Q8 BY CONDITION AND PARTY ####

Q8_diff_p <- matrix(NA, ncol = 9, nrow = 7)

## Create difference in means, CIs for republicans
diff_Q8_2_r <- svyby(~ Q8_d_rep, ~ ASSIGNMENT_A_2, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[1,1] <- diff_Q8_2_r[2,2] - diff_Q8_2_r[1,2]
Q8_diff_p[1,2] <- confint(diff_Q8_2_r)[2,1] - confint(diff_Q8_2_r)[1,1]
Q8_diff_p[1,3] <- confint(diff_Q8_2_r)[2,2] - confint(diff_Q8_2_r)[1,2]

diff_Q8_3_r <- svyby(~ Q8_d_rep, ~ ASSIGNMENT_A_3, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[2,1] <- diff_Q8_3_r[2,2] - diff_Q8_3_r[1,2]
Q8_diff_p[2,2] <- confint(diff_Q8_3_r)[2,1] - confint(diff_Q8_3_r)[1,1]
Q8_diff_p[2,3] <- confint(diff_Q8_3_r)[2,2] - confint(diff_Q8_3_r)[1,2]

diff_Q8_4_r <- svyby(~ Q8_d_rep, ~ ASSIGNMENT_A_4, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[3,1] <- diff_Q8_4_r[2,2] - diff_Q8_4_r[1,2]
Q8_diff_p[3,2] <- confint(diff_Q8_4_r)[2,1] - confint(diff_Q8_4_r)[1,1]
Q8_diff_p[3,3] <- confint(diff_Q8_4_r)[2,2] - confint(diff_Q8_4_r)[1,2]

diff_Q8_5_r <- svyby(~ Q8_d_rep, ~ ASSIGNMENT_A_5, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[4,1] <- diff_Q8_5_r[2,2] - diff_Q8_5_r[1,2]
Q8_diff_p[4,2] <- confint(diff_Q8_5_r)[2,1] - confint(diff_Q8_5_r)[1,1]
Q8_diff_p[4,3] <- confint(diff_Q8_5_r)[2,2] - confint(diff_Q8_5_r)[1,2]

diff_Q8_6_r <- svyby(~ Q8_d_rep, ~ ASSIGNMENT_A_6, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[5,1] <- diff_Q8_6_r[2,2] - diff_Q8_6_r[1,2]
Q8_diff_p[5,2] <- confint(diff_Q8_6_r)[2,1] - confint(diff_Q8_6_r)[1,1]
Q8_diff_p[5,3] <- confint(diff_Q8_6_r)[2,2] - confint(diff_Q8_6_r)[1,2]

diff_Q8_7_r <- svyby(~ Q8_d_rep, ~ ASSIGNMENT_A_7, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[6,1] <- diff_Q8_7_r[2,2] - diff_Q8_7_r[1,2]
Q8_diff_p[6,2] <- confint(diff_Q8_7_r)[2,1] - confint(diff_Q8_7_r)[1,1]
Q8_diff_p[6,3] <- confint(diff_Q8_7_r)[2,2] - confint(diff_Q8_7_r)[1,2]

diff_Q8_8_r <- svyby(~ Q8_d_rep, ~ ASSIGNMENT_A_8, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[7,1] <- diff_Q8_8_r[2,2] - diff_Q8_8_r[1,2]
Q8_diff_p[7,2] <- confint(diff_Q8_8_r)[2,1] - confint(diff_Q8_8_r)[1,1]
Q8_diff_p[7,3] <- confint(diff_Q8_8_r)[2,2] - confint(diff_Q8_8_r)[1,2]

## Create difference in means, CIs for independents
diff_Q8_2_i <- svyby(~ Q8_d_ind, ~ ASSIGNMENT_A_2, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[1,4] <- diff_Q8_2_i[2,2] - diff_Q8_2_i[1,2]
Q8_diff_p[1,5] <- confint(diff_Q8_2_i)[2,1] - confint(diff_Q8_2_i)[1,1]
Q8_diff_p[1,6] <- confint(diff_Q8_2_i)[2,2] - confint(diff_Q8_2_i)[1,2]

diff_Q8_3_i <- svyby(~ Q8_d_ind, ~ ASSIGNMENT_A_3, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[2,4] <- diff_Q8_3_i[2,2] - diff_Q8_3_i[1,2]
Q8_diff_p[2,5] <- confint(diff_Q8_3_i)[2,1] - confint(diff_Q8_3_i)[1,1]
Q8_diff_p[2,6] <- confint(diff_Q8_3_i)[2,2] - confint(diff_Q8_3_i)[1,2]

diff_Q8_4_i <- svyby(~ Q8_d_ind, ~ ASSIGNMENT_A_4, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[3,4] <- diff_Q8_4_i[2,2] - diff_Q8_4_i[1,2]
Q8_diff_p[3,5] <- confint(diff_Q8_4_i)[2,1] - confint(diff_Q8_4_i)[1,1]
Q8_diff_p[3,6] <- confint(diff_Q8_4_i)[2,2] - confint(diff_Q8_4_i)[1,2]

diff_Q8_5_i <- svyby(~ Q8_d_ind, ~ ASSIGNMENT_A_5, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[4,4] <- diff_Q8_5_i[2,2] - diff_Q8_5_i[1,2]
Q8_diff_p[4,5] <- confint(diff_Q8_5_i)[2,1] - confint(diff_Q8_5_i)[1,1]
Q8_diff_p[4,6] <- confint(diff_Q8_5_i)[2,2] - confint(diff_Q8_5_i)[1,2]

diff_Q8_6_i <- svyby(~ Q8_d_ind, ~ ASSIGNMENT_A_6, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[5,4] <- diff_Q8_6_i[2,2] - diff_Q8_6_i[1,2]
Q8_diff_p[5,5] <- confint(diff_Q8_6_i)[2,1] - confint(diff_Q8_6_i)[1,1]
Q8_diff_p[5,6] <- confint(diff_Q8_6_i)[2,2] - confint(diff_Q8_6_i)[1,2]

diff_Q8_7_i <- svyby(~ Q8_d_ind, ~ ASSIGNMENT_A_7, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[6,4] <- diff_Q8_7_i[2,2] - diff_Q8_7_i[1,2]
Q8_diff_p[6,5] <- confint(diff_Q8_7_i)[2,1] - confint(diff_Q8_7_i)[1,1]
Q8_diff_p[6,6] <- confint(diff_Q8_7_i)[2,2] - confint(diff_Q8_7_i)[1,2]

diff_Q8_8_i <- svyby(~ Q8_d_ind, ~ ASSIGNMENT_A_8, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[7,4] <- diff_Q8_8_i[2,2] - diff_Q8_8_i[1,2]
Q8_diff_p[7,5] <- confint(diff_Q8_8_i)[2,1] - confint(diff_Q8_8_i)[1,1]
Q8_diff_p[7,6] <- confint(diff_Q8_8_i)[2,2] - confint(diff_Q8_8_i)[1,2]

## Create difference in means, CIs for democrats
diff_Q8_2_d <- svyby(~ Q8_d_dem, ~ ASSIGNMENT_A_2, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[1,7] <- diff_Q8_2_d[2,2] - diff_Q8_2_d[1,2]
Q8_diff_p[1,8] <- confint(diff_Q8_2_d)[2,1] - confint(diff_Q8_2_d)[1,1]
Q8_diff_p[1,9] <- confint(diff_Q8_2_d)[2,2] - confint(diff_Q8_2_d)[1,2]

diff_Q8_3_d <- svyby(~ Q8_d_dem, ~ ASSIGNMENT_A_3, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[2,7] <- diff_Q8_3_d[2,2] - diff_Q8_3_d[1,2]
Q8_diff_p[2,8] <- confint(diff_Q8_3_d)[2,1] - confint(diff_Q8_3_d)[1,1]
Q8_diff_p[2,9] <- confint(diff_Q8_3_d)[2,2] - confint(diff_Q8_3_d)[1,2]

diff_Q8_4_d <- svyby(~ Q8_d_dem, ~ ASSIGNMENT_A_4, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[3,7] <- diff_Q8_4_d[2,2] - diff_Q8_4_d[1,2]
Q8_diff_p[3,8] <- confint(diff_Q8_4_d)[2,1] - confint(diff_Q8_4_d)[1,1]
Q8_diff_p[3,9] <- confint(diff_Q8_4_d)[2,2] - confint(diff_Q8_4_d)[1,2]

diff_Q8_5_d <- svyby(~ Q8_d_dem, ~ ASSIGNMENT_A_5, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[4,7] <- diff_Q8_5_d[2,2] - diff_Q8_5_d[1,2]
Q8_diff_p[4,8] <- confint(diff_Q8_5_d)[2,1] - confint(diff_Q8_5_d)[1,1]
Q8_diff_p[4,9] <- confint(diff_Q8_5_d)[2,2] - confint(diff_Q8_5_d)[1,2]

diff_Q8_6_d <- svyby(~ Q8_d_dem, ~ ASSIGNMENT_A_6, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[5,7] <- diff_Q8_6_d[2,2] - diff_Q8_6_d[1,2]
Q8_diff_p[5,8] <- confint(diff_Q8_6_d)[2,1] - confint(diff_Q8_6_d)[1,1]
Q8_diff_p[5,9] <- confint(diff_Q8_6_d)[2,2] - confint(diff_Q8_6_d)[1,2]

diff_Q8_7_d <- svyby(~ Q8_d_dem, ~ ASSIGNMENT_A_7, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[6,7] <- diff_Q8_7_d[2,2] - diff_Q8_7_d[1,2]
Q8_diff_p[6,8] <- confint(diff_Q8_7_d)[2,1] - confint(diff_Q8_7_d)[1,1]
Q8_diff_p[6,9] <- confint(diff_Q8_7_d)[2,2] - confint(diff_Q8_7_d)[1,2]

diff_Q8_8_d <- svyby(~ Q8_d_dem, ~ ASSIGNMENT_A_8, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p[7,7] <- diff_Q8_8_d[2,2] - diff_Q8_8_d[1,2]
Q8_diff_p[7,8] <- confint(diff_Q8_8_d)[2,1] - confint(diff_Q8_8_d)[1,1]
Q8_diff_p[7,9] <- confint(diff_Q8_8_d)[2,2] - confint(diff_Q8_8_d)[1,2]

## Plot
Q8_plot_p.df <- data.frame(rep = Q8_diff_p[,1],
                           rep_lo = Q8_diff_p[,2],
                           rep_hi = Q8_diff_p[,3],
                           ind = Q8_diff_p[,4],
                           ind_lo = Q8_diff_p[,5],
                           ind_hi = Q8_diff_p[,6],
                           dem = Q8_diff_p[,7],
                           dem_lo = Q8_diff_p[,8],
                           dem_hi = Q8_diff_p[,9],
                           dcond = c(1,5,9,13,17,21,25),
                           icond = c(2,6,10,14,18,22,26),
                           rcond = c(3,7,11,15,19,23,27))

Q8_plot_p <- ggplot(Q8_plot_p.df) + 
  geom_pointrange(aes(x = rcond, y = rep, ymin = rep_lo, ymax = rep_hi), color = "red") +
  geom_pointrange(aes(x = icond, y = ind, ymin = ind_lo, ymax = ind_hi), color = "gray60") +
  geom_pointrange(aes(x = dcond, y = dem, ymin = dem_lo, ymax = dem_hi), color = "blue") +
  geom_text(aes(x = rcond, y = rep, label = sprintf("%0.2f", round(rep, digits = 2))), size = 3,
            vjust = 2) +
  geom_text(aes(x = icond, y = ind, label = sprintf("%0.2f", round(ind, digits = 2))), size = 3,
            vjust = 2) +
  geom_text(aes(x = dcond, y = dem, label = sprintf("%0.2f", round(dem, digits = 2))), size = 3,
            vjust = 2)
Q8_plot_p <- Q8_plot_p + xlab("Treatment Condition") + 
  scale_x_continuous(breaks = c(2, 6, 10, 14, 18, 22, 26),
                     labels = addline_format(c("Republican Military", "Democrat Military",
                                               "Polarized Military", "Lost Afghanistan War",
                                               "Won Afghanistan War", "Ethical Generals",
                                               "Unethical Generals"))) +
  ylab("Difference in Means from Control Condition")
Q8_plot_p <- Q8_plot_p + geom_hline(yintercept = 0, colour = "black", lty = 1) +
  ggtitle("Q8 by Condition and Party ID") 
Q8_plot_p + theme_bw()

## Second plot - partisan conditions only (for Peter's Oxford talk)

Q8_diff_p2 <- svyby(~ Q8_d, ~ party + DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)
Q8_diff_p2_dem <- subset(Q8_diff_p2, party == 0)
Q8_diff_p2_ind <- subset(Q8_diff_p2, party == 1)
Q8_diff_p2_rep <- subset(Q8_diff_p2, party == 2)

Q8_party <- matrix(NA, ncol = 9, nrow = 3) # create difference in means
for (i in 2:4) {
  Q8_party[i-1,7] <- Q8_diff_p2_dem$Q8_d[[i]] - Q8_diff_p2_dem$Q8_d[1]
  Q8_party[i-1,4] <- Q8_diff_p2_ind$Q8_d[[i]] - Q8_diff_p2_ind$Q8_d[1]
  Q8_party[i-1,1] <- Q8_diff_p2_rep$Q8_d[[i]] - Q8_diff_p2_rep$Q8_d[1]
}

Q8_diff_p2_dem$ci_lo <- Q8_diff_p2_dem$Q8_d - 1.96 * Q8_diff_p2_dem$se
Q8_diff_p2_dem$ci_hi <- Q8_diff_p2_dem$Q8_d + 1.96 + Q8_diff_p2_dem$se
Q8_diff_p2_ind$ci_lo <- Q8_diff_p2_ind$Q8_d - 1.96 * Q8_diff_p2_ind$se
Q8_diff_p2_ind$ci_hi <- Q8_diff_p2_ind$Q8_d + 1.96 * Q8_diff_p2_ind$se
Q8_diff_p2_rep$ci_lo <- Q8_diff_p2_rep$Q8_d - 1.96 * Q8_diff_p2_rep$se
Q8_diff_p2_rep$ci_hi <- Q8_diff_p2_rep$Q8_d + 1.96 * Q8_diff_p2_rep$se

for (i in 2:4) { # add confidence intervals
  Q8_party[i-1,8] <- Q8_diff_p2_dem$ci_lo[[i]] - Q8_diff_p2_dem$ci_lo[1]
  Q8_party[i-1,9] <- Q8_diff_p2_dem$ci_hi[[i]] - Q8_diff_p2_dem$ci_hi[1]
  Q8_party[i-1,2] <- Q8_diff_p2_rep$ci_lo[[i]] - Q8_diff_p2_rep$ci_lo[1]
  Q8_party[i-1,3] <- Q8_diff_p2_rep$ci_hi[[i]] - Q8_diff_p2_rep$ci_hi[1]
  Q8_party[i-1,5] <- Q8_diff_p2_ind$ci_lo[[i]] - Q8_diff_p2_ind$ci_lo[1]
  Q8_party[i-1,6] <- Q8_diff_p2_ind$ci_hi[[i]] - Q8_diff_p2_ind$ci_hi[1]
}

Q8_plot_p2.df <- data.frame(rep = Q8_party[,1],
                            rep_lo = Q8_party[,2],
                            rep_hi = Q8_party[,3],
                            ind = Q8_party[,4],
                            ind_lo = Q8_party[,5],
                            ind_hi = Q8_party[,6],
                            dem = Q8_party[,7],
                            dem_lo = Q8_party[,8],
                            dem_hi = Q8_party[,9],
                            dcond = c(1,5,9),
                            icond = c(2,6,10),
                            rcond = c(3,7,11))

Q8_plot_p2 <- ggplot(Q8_plot_p2.df) + 
  geom_pointrange(aes(x = rcond, y = rep, ymin = rep_lo, ymax = rep_hi), color = "red") +
  geom_pointrange(aes(x = icond, y = ind, ymin = ind_lo, ymax = ind_hi), color = "gray60") +
  geom_pointrange(aes(x = dcond, y = dem, ymin = dem_lo, ymax = dem_hi), color = "blue") +
  geom_text(aes(x = rcond, y = rep, label = sprintf("%0.2f", round(rep, digits = 2))), size = 3,
            vjust = 2) +
  geom_text(aes(x = icond, y = ind, label = sprintf("%0.2f", round(ind, digits = 2))), size = 3,
            vjust = 2) +
  geom_text(aes(x = dcond, y = dem, label = sprintf("%0.2f", round(dem, digits = 2))), size = 3,
            vjust = 2)
Q8_plot_p2 <- Q8_plot_p2 + xlab("Treatment Condition") + 
  scale_x_continuous(breaks = c(2, 6, 10),
                     labels = addline_format(c("Republican Military", "Democrat Military",
                                               "Polarized Military"))) +
  ylab("Difference in Means from Control Condition")
Q8_plot_p2 <- Q8_plot_p2 + geom_hline(yintercept = 0, colour = "black", lty = 1) +
  theme_bw()
ggsave("~/Dropbox/Public_Conf_Mil/Wave 1 Figures/Q8_partisan2.jpeg", 
       plot = Q8_plot_p2, dpi = 1000)
